gskpango: Don't create text nodes for clipped text
authorTimm Bäder <mail@baedert.org>
Sun, 4 Mar 2018 16:48:36 +0000 (17:48 +0100)
committerTimm Bäder <mail@baedert.org>
Sun, 4 Mar 2018 18:15:03 +0000 (19:15 +0100)
Measure the text here directly and check if the created node bounds will
be clipped away before even creating the text node.

gtk/gskpango.c

index 2021e92ae8915c971875d340e8f4146af3c68c5d..04b58f566eaed0d8e0b4de436c86b54d88fa8729 100644 (file)
@@ -115,11 +115,43 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
   int x_offset, y_offset;
   GskRenderNode *node;
   GdkRGBA color;
+  graphene_rect_t node_bounds;
+  PangoRectangle ink_rect;
+
+  pango_glyph_string_extents (glyphs, font, &ink_rect, NULL);
+  pango_extents_to_pixels (&ink_rect, NULL);
+
+  /* Don't create empty nodes */
+  if (ink_rect.width == 0 || ink_rect.height == 0)
+    return;
 
   gtk_snapshot_get_offset (crenderer->snapshot, &x_offset, &y_offset);
+
+  graphene_rect_init (&node_bounds,
+                      x_offset + (float)x/PANGO_SCALE,
+                      y_offset + (float)y/PANGO_SCALE + ink_rect.y,
+                      ink_rect.x + ink_rect.width,
+                      ink_rect.height);
+
+  /* Remove the snapshot offset from the position here again since
+   * gtk_snapshot_clips_rect will apply it to the given rect. */
+  if (gtk_snapshot_clips_rect (crenderer->snapshot,
+                               &(cairo_rectangle_int_t){
+                                 node_bounds.origin.x - x_offset,
+                                 node_bounds.origin.y - y_offset,
+                                 ceil (node_bounds.size.width),
+                                 ceil (node_bounds.size.height)
+                               }))
+    return;
+
   get_color (crenderer, PANGO_RENDER_PART_FOREGROUND, &color);
 
-  node = gsk_text_node_new (font, glyphs, &color, x_offset + (double)x/PANGO_SCALE, y_offset + (double)y/PANGO_SCALE);
+  node = gsk_text_node_new_with_bounds (font,
+                                        glyphs,
+                                        &color,
+                                        x_offset + (double)x/PANGO_SCALE,
+                                        y_offset + (double)y/PANGO_SCALE,
+                                        &node_bounds);
   if (node == NULL)
     return;